#!/usr/bin/env python
# -*- encoding: utf-8 -*-
"""
@File        :   203.移除链表元素.py    
@Contact     :   lhouctcs@163.com
@License     :   MIT
@Time        :   2023/2/4 12:48
@Author      :   lhou 
@Version     :   0.1.0
@Description :
"""
from typing import Optional


# import lib
class ListNode:
    def __init__(self, val=0, next=None):
        self.val = val
        self.next = next


class Solution:
    def removeElements(self, head: Optional[ListNode], val: int) -> Optional[ListNode]:
        root = ListNode(-1)
        root.next = head
        node = root
        while node and node.next:
            if node.next.val == val:
                node.next = node.next.next
            else:
                node = node.next
        return root.next
